package com.chenyi.leetcode.editor.cn;

import java.util.Arrays;

/**
 * [66]加一
 *
 * @author by chenyi
 * @className PlusOne
 * @date 2022-06-21 23:12:05
 */
public class PlusOne {
    public static void main(String[] args) {
        Solution solution = new PlusOne().new Solution();
        System.out.println(Arrays.toString(solution.plusOne(new int[]{1, 9, 9})));
    }

    @SuppressWarnings("AlibabaCommentsMustBeJavadocFormat")
            //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int[] plusOne(int[] digits) {
//            for (int i = digits.length - 1; i >= 0; i--) {
//                // 可以不用判断是否为9，我们可以判断最后一位是否为0
//                digits[i] = (digits[i] + 1) % 10;
//                if (digits[i] != 0) {
//                    return digits;
//                }
//            }
//            digits = new int[digits.length + 1];
//            digits[0] = 1;
//            return digits;
            // 判断是否为9
            int n = digits.length;
            for (int i = n - 1; i >= 0; --i) {
                if (digits[i] != 9) {
                    ++digits[i];
                    for (int j = i + 1; j < n; ++j) {
                        digits[j] = 0;
                    }
                    return digits;
                }
            }

            // digits 中所有的元素均为 9
            int[] ans = new int[n + 1];
            ans[0] = 1;
            return ans;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
